var ProcessQcDetailModel = function (detail) {
  BaseModel.call(this);
  var self = this;

  var imgFields = ['img_1', 'img_2', 'img_3', 'img_4'];
  var fn = {
    initData: function (detail) {
      if (detail === undefined) {
        self.content = '';
        self.defalutValue = '';
        self.imgs = [];
        self.video = '';
        return;
      }

      var fields = ['content', 'defalutValue', 'video'];
      self.setDataFromObj(detail, fields, '');
    }
  };

  this.checkValid = function () {
    return this.checkRequiredFields(['content']);
  };

  this.getObject = function () {
    var postData = {};
    var fields = ['content', 'defalutValue', 'video'];
    this.setPostData(postData, fields, false);
    return postData;
  };

  fn.initData(detail);
};

var ProcessQcFormModel = function (workProcess, data) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (workProcess, data) {
      self.processId = workProcess.id;
      self.processName = workProcess.processName;

      var details = data.qcList;
      if (SysUtil.isEmpty(details) || details[0].content === '') {
        self.selfCheckTime = undefined;
        self.siteCheckTime = undefined;
        self.detailList = [];
      } else {
        details = _.orderBy(details, ['rowIndex'], ['asc']);
        var detailList = [];
        for (var i = 0; i < details.length; i++) {
          var detail = details[i];
          if (detail.content === '') {
            continue;
          }
          detail = new ProcessQcDetailModel(detail);
          detailList.push(detail);
        }
        self.detailList = detailList;
        self.selfCheckTime = data.selfCheckTime;
        self.siteCheckTime = data.siteCheckTime;
      }
    }
  };

  this.addDetail = function (detail) {
    this.detailList.push(detail);
  };

  this.updateDetail = function (newDetail, oldDetail) {
    var detailList = this.detailList;
    var index = detailList.indexOf(oldDetail);
    if (index > -1) {
      detailList.splice(index, 1, newDetail);
    }
  };

  this.deleteDetail = function (detail) {
    var detailList = this.detailList;
    var index = detailList.indexOf(detail);
    if (index > -1) {
      detailList.splice(index, 1);
    }
  };

  this.checkValid = function () {
    var code = this.checkRequiredFields(['selfCheckTime', 'siteCheckTime']);
    if (code !== '') {
      return code;
    }

    if (this.selfCheckTime === 0) {
      return 'selfCheckTimeIsZero';
    }

    if (this.siteCheckTime === 0) {
      return 'siteCheckTimeIsZero';
    }

    var detailList = this.detailList;
    if (detailList.length === 0) {
      return 'detailListIsNull';
    }
    
    var details = _.uniqBy(detailList, 'content');
    if (details.length !== detailList.length){
      return 'detailListRepeat';
    }

    return '';
  };

  this.getObject = function () {
    var postData = {};
    var fields = ['processId', 'selfCheckTime', 'siteCheckTime'];
    this.setPostData(postData, fields, false);

    var detailList = this.detailList;
    var details = [];
    for (var i = 0; i < detailList.length; i++) {
      var detail = detailList[i];
      detail = detail.getObject();
      details.push(detail);
      detail.rowIndex = i + 1;
    }
    postData.QCDetailArray = JSON.stringify(details);
    return postData;
  }

  fn.initData(workProcess, data);
};
